<?
if (!defined('SMF'))
	die('Hacking attempt...');

global $defence_degrade_rate, $energy_per_fighter;
	
$message .= '<p><b>Degrading Sector Fighters with no friendly base</b></p>';

// Run the degrade mechanism based on the current multiplier
for ($cycles = 1; $cycles <= $multiplier; $cycles++)
{
	$message .= '<p>Run ' . $cycles . '</p>';
	
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_sector_defence}
		WHERE defence_type = "F"',
		array(
			'db_sector_defence' => $dbtables['sector_defence']
		)
	);
	
	while ($row = $smcFunc['db_fetch_assoc']($result))
	{
		$result2 = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_ships}
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'ship_id' => $row['ship_id']
			)
		);
		$sched_playerinfo = $smcFunc['db_fetch_assoc']($result2);
		$smcFunc['db_free_result']($result2);
		
		$result2 = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_planets}
			WHERE (owner = {int:ship_id} OR (corp = {int:team} AND {int:team} <> 0))
			AND sector_id = {int:sector_id}
			AND energy > 0',
			array(
				'db_planets' => $dbtables['planets'],
				'ship_id' => $row['ship_id'],
				'team' => $sched_playerinfo['team'],
				'sector_id' => $row['sector_id']
			)
		);

		if ($smcFunc['db_num_rows']($result2) == 0)
		{
			$update = $smcFunc['db_query']('', '
				UPDATE {raw:db_sector_defence}
				SET quantity = quantity - GREATEST(ROUND(quantity * {float:defence_degrade_rate}),1)
				WHERE defence_id = {int:defence_id}
				AND quantity > 0',
				array(
					'db_sector_defence' => $dbtables['sector_defence'],
					'defence_degrade_rate' => $defence_degrade_rate,
					'defence_id' => $row['defence_id']
					
				)
			);
			$degrade_rate = $defence_degrade_rate * 100;
			playerlog($row['ship_id'], LOG_DEFENCE_DEGRADE, "$row[sector_id]|$degrade_rate");
		}
		else
		{
			$energy_required = ROUND($row['quantity'] * $energy_per_fighter);
			
			$result3 = $smcFunc['db_query']('', '
				SELECT IFNULL(SUM(energy),0) AS energy_available
				FROM {raw:db_planets}
				WHERE (owner = {int:ship_id} OR (corp = {int:team} AND {int:team} <> 0))
				AND sector_id = {int:sector_id}',
				array(
					'db_planets' => $dbtables['planets'],
					'ship_id' => $row['ship_id'],
					'team' => $sched_playerinfo['team'],
					'sector_id' => $row['sector_id']
					
				)
			);
			$planet_energy = $smcFunc['db_fetch_assoc']($result3);
			$smcFunc['db_free_result']($result3);

			$energy_available = $planet_energy['energy_available'];
			
			$message .= 'available energy: ' . $energy_available . ', required energy: ' . $energy_required . '.<br>';
			
			if ($energy_available > $energy_required)
			{
				while ($degrade_row = $smcFunc['db_fetch_assoc']($result2))
				{
					$update = $smcFunc['db_query']('', '
						UPDATE {raw:db_planets}
						SET energy = energy - GREATEST(ROUND({float:energy_required} * (energy / {float:energy_available})),1)
						WHERE planet_id = {int:planet_id}',
						array(
							'db_planets' => $dbtables['planets'],
							'energy_required' => $energy_required,
							'energy_available' => $energy_available,
							'planet_id' => $degrade_row['planet_id']
							
						)
					);
				}
			}
			else
			{
				$update = $smcFunc['db_query']('', '
					UPDATE {raw:db_sector_defence}
					SET quantity = quantity - GREATEST(ROUND(quantity * {float:defence_degrade_rate}),1)
					WHERE defence_id = {int:defence_id}',
					array(
						'db_sector_defence' => $dbtables['sector_defence'],
						'defence_degrade_rate' => $defence_degrade_rate,
						'defence_id' => $row['defence_id']
						
					)
				);
				
				$degrade_rate = $defence_degrade_rate * 100;
				playerlog($row['ship_id'], LOG_DEFENCE_DEGRADE, "$row[sector_id]|$degrade_rate");  
			}
		}
		$smcFunc['db_free_result']($result2);
	}
	$smcFunc['db_free_result']($result);
	
	$delete = $smcFunc['db_query']('', '
		DELETE FROM {raw:db_sector_defence}
		WHERE quantity <= 0',
		array(
			'db_sector_defence' => $dbtables['sector_defence']
		)
	);
}
?>
